// pages/storenearby/storenearby.js
var utils = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentPage: 1,
    num: "",
    longitude: null,
    latitude: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.getLocation({
      success: function(res) {
        console.log(res)
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
        }, function() {
          that.productList(that.data.currentPage);
        })
      },
    })
  },
  productList: function(currentPage) {
    var that = this;
    utils.showLoading();
    utils.http("/api/v1/store/store_product", {
      page: currentPage,
      latitude: that.data.latitude,
      longitude: that.data.longitude,
    }, function(res) {
      utils.hideLoading();
      var allpost = that.data.product;
      if (allpost == undefined) {
        allpost = [];
      }
      for (var i in res.datas) {
        res.datas[i].number = 0;
        allpost.push(res.datas[i]);
      }
      if (res.datas.length < 10) {
        that.setData({
          tishi_title: '已加载全部',
        });
      } else {
        that.setData({
          tishi_title: '加载更多',
        });
      }
      that.setData({
        product: allpost,
        ready: res.datas.length == 0 ? 1 : 2,
      })
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    if (that.data.ready == 2) {
      var page = that.data.currentPage + 1
      console.log(page)
      that.setData({
        currentPage: page,
      })
      that.productList(page);
    }

  },
  //商品详情  //立即购买页面
  noFrutisDetailsTap: function(e) {
    var buyType = 1
    wx.navigateTo({
      url: '../commoditydetails/commoditydetails?buyType=' + buyType,
    })
  },

  //立即购买
  buyNowTap: function(e) {
    //没有产地直发
    var id = e.currentTarget.dataset.id;
    var storeid = e.currentTarget.dataset.storeid;
    var price = e.currentTarget.dataset.price;
    var num = e.currentTarget.dataset.num;
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    var subtitle = e.currentTarget.dataset.subtitle;
    var image = e.currentTarget.dataset.image;
    wx.navigateTo({
      url: '../storeconfirmorder/storeconfirmorder?id=' + id + "&storeid=" + storeid + "&num=" + num + "&name=" + name + "&subtitle=" + subtitle + "&price=" + price + "&image=" + image
    })
  },
  //加
  onPrevTap: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.data.product[index].number = that.data.product[index].number + 1
    that.setData({
      product: that.data.product
    })
  },
  //减
  onNextTap: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.data.product[index].number = that.data.product[index].number - 1
    that.setData({
      product: that.data.product
    })
  }
})